import 'dart:async';

import 'package:leader_help/entrance/route/routes.dart';
import 'package:leader_help/utils/component/PrivacyView.dart';
import 'package:leader_help/utils/storage.dart';
import 'package:get/get.dart';

import 'index.dart';

class SplashLogic extends GetxController {
  SplashLogic();

  final state = SplashState();

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
    state.timer = Timer.periodic(const Duration(seconds: 1), (_) {
      if (state.seconds == 0) {
        toMain();
      }
      state.seconds -= 1;

      if (state.seconds >= 0) {
        update();
      }
    });
  }

  void toMain() async {
    state.timer?.cancel();
    await SpUtil().init();
    var isFirstLaunch = SpUtil().getBool(StorageKeys.firstLaunch);
    if (isFirstLaunch == false) {
      Get.offAndToNamed(RouterPath.main);
    } else {
      // Get.offAndToNamed(RouterPath.welcome);
      // SpUtil().setBool(StorageKeys.firstLaunch, false);
      showPrivacyDialog();
    }
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
    state.timer?.cancel();
    state.timer = null;
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
